package simpleGeom;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class SimpleObjLoaderStream {
	 DataInputStream in;
	  BufferedReader br;
	  

public float[] readVertex(){
	String strLine;
	try {
		strLine = br.readLine();
		if (strLine==null)return null;
		while (!strLine.startsWith("v") ){
			strLine = br.readLine();
			if (strLine==null)return null;
		}
		
			String[] data=strLine.split(" ");
			float[] coordinates=new float[3];
			for (int i=0;i<3;i++){
				coordinates[i]=Float.valueOf(data[i+1]);
			}
			return coordinates;
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return null;
	}
}

public int[] readFace(){
	String strLine;
	try {
		strLine = br.readLine();
		while (strLine!=null && !strLine.startsWith("f") ){
			strLine = br.readLine();
		}
		if (strLine==null)return null;
		else {
			String[] data=strLine.split(" ");
			int[] coordinates=new int[data.length-1];
			for (int i=0;i<coordinates.length;i++){
				coordinates[i]=Integer.valueOf(data[i+1]);
			}
			return coordinates;
		}
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return null;
	}
}

public void close(){
	 try {
		in.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

public void init(String file){
	
	 try{
		  // Open the file that is the first 
		  // command line parameter
		  FileInputStream fstream = new FileInputStream(file);
		  // Get the object of DataInputStream
		   in = new DataInputStream(fstream);
		   br = new BufferedReader(new InputStreamReader(in));
		  
	  }catch (Exception e){//Catch exception if any
		  System.err.println("Error: " + e.getMessage());
		  }
}
}
